package class10_tree;

/**
 * 二叉树：
 * 递归序遍历（先序，中序，后序）
 * 先序：头 左 右
 * 中序：左 头 右
 * 后续：左 右 头
 */
public class Class02 {

    // 定义Node类
    static class Node {
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }
    }

    /**
     * 给定一棵树的头节点，进行先序遍历打印
     *
     * @param head
     */
    public static void pre(Node head) {
        if (head == null) {
            return;
        }
        System.out.println(head.value);
        pre(head.left);
        pre(head.right);
    }

    /**
     * 给定一棵树的头节点，进行中序遍历打印
     *
     * @param head
     */
    public static void in(Node head) {
        if (head == null) {
            return;
        }
        in(head.left);
        System.out.println(head.value);
        in(head.right);
    }

    /**
     * 给定一棵树的头节点，进行后序遍历打印
     *
     * @param head
     */
    public static void pos(Node head) {
        if (head == null) {
            return;
        }
        pos(head.left);
        pos(head.right);
        System.out.println(head.value);
    }

    /**
     * 递归序遍历：在递归的过程中，没一个节点都会被来到三次
     *
     * @param node
     */
    public static void f(Node node) {
        if (node == null) {
            return;
        }
        // 1
        f(node.left);
        // 2
        f(node.right);
        // 3
    }

    public static void main(String[] args) {
        Node node = new Node(1);
        node.left = new Node(2);
        node.right = new Node(3);
        node.left.left = new Node(4);
        node.left.right = new Node(5);
        node.right.left = new Node(6);
        node.right.right = new Node(7);

        pre(node);
        System.out.println("===============");
        in(node);
        System.out.println("===============");
        pos(node);

    }


}
